package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.*;
import com.example.cdctest.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/manage/examination")
public class ManageExaminationController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private ExaminationService examinationService;

    @Resource
    private TestPaperStructureService testPaperStructureService;

    @Resource
    private ParticipantsService participantsService;

    //查出所有类别，并查出每种类别对应的分值
    @RequestMapping("add-examination-message")
    public ModelAndView addExaminationMessage(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        ServletContext servletContext=request.getSession().getServletContext();
        List<Config> configList=(List<Config>)servletContext.getAttribute("configs");
        modelAndView.addObject("configList",configList);
        modelAndView.addObject("categorys",categoryService.findAll());
        modelAndView.setViewName("/manage/examination/add-examination-message");
        return modelAndView;
    }

    //取出所有的内测用户
    @RequestMapping("formal-user")
    public ModelAndView formalUser(Integer examinationId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("examinationId",examinationId);
        modelAndView.addObject("userInfos",userInfoService.selectFormalUser());
        modelAndView.setViewName("/manage/examination/formal-user");
        return modelAndView;
    }

    //查看选中用户
    @RequestMapping("select-examination-user")
    @ModelAttribute("participantsList")
    @ResponseBody
    public List<Participants> selectExaminationUser(Integer examinationId){
        return participantsService.selectSelectParticipants(examinationId);
    }

    //根据选中的用户Id,以及考试信息,添加考核人员
    @RequestMapping("batch-select-user")
    @ResponseBody
    public void batchSelectUser(String[] ids,Integer examinationId)
    {
       userInfoService.insertSelectUser(ids,examinationId);
    }

    //考试信息
    @RequestMapping("addExaminationTitle")
    @ResponseBody
    public Integer addExaminationTitle(String examinationTitle, Date startTime, Date endTime, HttpServletRequest request){
        Admin loginAdmin=(Admin)request.getSession().getAttribute("loginAdmin");
        Integer id=examinationService.insertExamination(examinationTitle,startTime,endTime,loginAdmin);
        return id;
    }

    //添加考题结构
    @RequestMapping("addTestPaperStructure")
    @ResponseBody
    public void addTestPaperStructure(String categordIds,String categorySums,Integer examinationId){
        String[] categordId=categordIds.split(",");
        String[] categorySum=categorySums.split(",");
        testPaperStructureService.insertTestPaperStructure(categordId,categorySum,examinationId);
    }

    //删除考试参与人信息
    @RequestMapping("updateParticipants")
    @ResponseBody
    public void updateParticipants(Integer id){
        participantsService.updateParticipants(id);
    }

    //转到考试信息表界面
    @GetMapping("examination-message-list")
    public void examinationMessageList(){ }

    //根据条件查出数据,页码,开始日期，截止日期，关键条件
    @RequestMapping("examination-manage-list-hh")
    public ModelAndView examinationMessageDataList(Date logmin,Date logmax,String text,Integer pageNum,HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        if(pageNum==null) pageNum=1;
        Integer pageSize=10;
        ServletContext servletContext=request.getSession().getServletContext();
        List<Config> configList=(List<Config>) servletContext.getAttribute("configs");
        for (Config config : configList) {
            if("manage_examination_count".equals(config.getKey())){
                pageSize=Integer.parseInt(config.getValue());
            }
        }
        modelAndView.addObject("pageInfo",examinationService.findAllExamination(logmin,logmax,text,pageNum,pageSize));
        modelAndView.addObject("examinationCount",examinationService.getExamination(logmin,logmax,text,pageNum,pageSize));
        modelAndView.setViewName("/manage/examination/examination-message-data-list");
        return modelAndView;
    }

    //根据考试信息的Id,查出该考试的考题结构
    @RequestMapping("test-paper-structure-list")
    @ModelAttribute("testPaperStructureList")
    public List<TestPaperStructure> testPaperStructure(Integer examinationId){
        return examinationService.selectTestPaperStructureById(examinationId);
    }

    //根据考试信息的Id，查出该考试的所有参加考试人员
    @RequestMapping("examination-participants-list")
    @ModelAttribute("testPaperList")
    public List<TestPaper> examinationParticipantsList(Integer examinationId){
        System.out.println(examinationId);
        return examinationService.selectExaminationParticipantsById(examinationId);
    }

    //根据考试信息的Id，删除该考试信息
    @RequestMapping("updateExaminationById")
    @ResponseBody
    public void updateExaminationById(Integer id){
        examinationService.updateExaminationById(id);
    }
}
